home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / disk / openDisk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  4.0 KB  |  163 lines

  1. /*
  2.  *   $RCSfile: openDisk.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:38 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37. #include "sysdefs.h"
  38. #include <sys/stat.h>
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "tid.h"
  45. #include "io.h"
  46. #include "lock.h"
  47. #include "object.h"
  48. #include "msgdefs.h"
  49. #include "disk.h"
  50. #include "thread.h"
  51. #include "semaphore.h"
  52. #include "latch.h"
  53. #include "link.h"
  54. #include "bf.h"
  55. #include "volume.h"
  56. #include "pool.h"
  57. #include "threadstate.h"
  58. #include "thread_globals.h"
  59. #include "disk_funcs.h"
  60. #include "msg_globals.h"
  61. #include "queue_consist.h"
  62. #include "io_globals.h"
  63.  
  64.  
  65. static BOOL
  66. checkForRaw(VOLREC *const volRec, const BOOL couldBeEmpty) 
  67. {
  68.     struct stat        statInfo;
  69.  
  70.     /*
  71.      *  See if the named file is a raw disk
  72.      */
  73.     if (stat(volRec->volNameRec->volName.name, &statInfo) < 0) {
  74.         SM_ERROR(TYPE_SYS, errno);
  75.         return FALSE; /* not ok */
  76.     }
  77.     /* if it's not a character device, its not a raw disk */
  78.     if ((statInfo.st_mode & S_IFMT) != S_IFCHR) {
  79.         volRec->volflags &= ~VOL_RAWDEV;
  80.     } else {
  81.         volRec->volflags |= VOL_RAWDEV;
  82.     }
  83.  
  84.     if(couldBeEmpty)
  85.         return TRUE; /* ok */
  86.  
  87.     if( statInfo.st_size < sizeof(VOLHDR) && !(volRec->volflags & VOL_RAWDEV)) {
  88.         /* catch this before we try to read the header */
  89.         /* note that st_size is 0 for raw devices */
  90.         SM_ERROR(TYPE_USER, esmBADVOLHEADER);
  91.         return FALSE; /* not ok */
  92.     }
  93.     return TRUE; /* ok */
  94. }
  95.  
  96.  
  97. BOOL
  98. openDisk (
  99.     const    char    *const diskName,
  100.     const    BOOL    forFormat,
  101.     VOLREC             *const volRec
  102. )
  103.  
  104. {
  105.     FLAGS            flags;
  106.     DISKMSG            *message = Active->diskMessage;
  107.  
  108.     TRPRINT(TR_DISK, TR_LEVEL_1, ("opening disk:%s", diskName));
  109.     SM_ASSERT(LEVEL_1, (message!=NULL));
  110.  
  111.     SM_ASSERT(LEVEL_1, volRec->volLink == NULL);
  112.  
  113.     if(forFormat)
  114.         flags = O_CREAT | O_TRUNC;
  115.     else 
  116.         flags = NOFLAGS;
  117.  
  118.     /*
  119.      *    fork disk process.  But, don't fork it if we're only doing
  120.      * fastPath (e.g. formatvol is running or we're pre-mounting
  121.      * devices.)
  122.      */
  123.     if (FastPathOnly == FALSE)  {
  124.         /*
  125.          * Open the file to see if there's any reason not to
  126.          * fork a process for this volume.
  127.          */
  128.         int fd;
  129.  
  130.         if((fd = open(diskName, flags, 0604)) < 0){
  131.             fprintf(sm_ErrorStream, "%s: ", diskName);
  132.             SM_ERROR(TYPE_SYS, errno);
  133.             /* no go */
  134.             return FALSE;
  135.         }
  136.         close(fd);
  137.         TRPRINT(TR_DISK, TR_LEVEL_1, ("disk:%s is open-able", diskName));
  138.  
  139.         if (forkDiskProc(volRec) != esmNOERROR)    {
  140.             /* no go */
  141.             return FALSE;
  142.         } 
  143.     }
  144.  
  145.     /*
  146.      *    setup the open message
  147.      */
  148.     message->header.type = OPEN_DISK;
  149.     message->body.flags = flags;
  150.  
  151.     /*
  152.      *    send the message
  153.      */
  154.     if (callDisk(volRec, message))    {
  155.         /* callDisk returns esmNOERROR or an error # */
  156.         return FALSE;
  157.  
  158.     } 
  159.     /* ok ... opened it fine */
  160.  
  161.     return checkForRaw(volRec, forFormat); /* TRUE if ok; FALSE if not ok */
  162. }
  163.